package org.starsource.forum.client.widget;

import org.starsource.forum.client.widget.handler.ImageClickHandler;
import org.starsource.forum.shared.util.ImageContainerSupport;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.SimplePanel;

public class ImagePanel extends DecoratorPanel {

	private SimplePanel contentPanel = new SimplePanel();

	private Image image = new Image();

	public Image getImage() {
		return this.image;
	}

	private String imageUrl, thumbUrl;

	public String getImageUrl() {
		return this.imageUrl;
	}
	public String getThumbUrl() {
		return this.thumbUrl;
	}

	private int imageWidth, imageHeight;
	private int containerWidth, containerHeight;

	public int getImageWidth() {
		return this.imageWidth;
	}
	public int getImageHeight() {
		return this.imageHeight;
	}

	private ImageClickHandler imageClickHandler;
	public void setImageClickHandler(ImageClickHandler imageClickHandler) {
		this.imageClickHandler = imageClickHandler;
	}

	public ImagePanel() {
		contentPanel.setWidget(image);
		this.setWidget(contentPanel);

//		this.setStylePrimaryName("gwter-DecoratorPanel");
		contentPanel.setStylePrimaryName("center");
		image.setStylePrimaryName("image");

		image.setVisible(false);

		image.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				imageClickHandler.onImageClick(ImagePanel.this.imageUrl, ImagePanel.this.imageWidth, ImagePanel.this.imageHeight);
			}

		});
	}


	public ImagePanel(int containerWidth, int containerHeight, int imageWidth, int imageHeight) {
		this();
		this.setContainerSize(containerWidth, containerHeight);
		this.setImageSize(imageWidth, imageHeight);
	}
	public ImagePanel(int containerWidth, int containerHeight, int imageWidth, int imageHeight, String url) {
		this();
		this.setContainerSize(containerWidth, containerHeight);
		this.setImageSize(imageWidth, imageHeight);
		this.setImageUrl(url);
	}

	public ImagePanel(int containerWidth, int containerHeight, int imageWidth, int imageHeight, String imageUrl, String thumbUrl) {
		this();
		this.setContainerSize(containerWidth, containerHeight);
		this.setImageSize(imageWidth, imageHeight);
		this.setImageUrl(imageUrl, thumbUrl);
	}

	private void setImage(String url) {
		image.setUrl(url);

		ImageContainerSupport ratio = new ImageContainerSupport(
				this.imageWidth, this.imageHeight,
				this.containerWidth, this.containerHeight);
		ratio.calculateScale();
		image.setWidth(ratio.getScaleWidth() + "px");
		image.setHeight(ratio.getScaleHeight() + "px");
		image.setVisible(true);
	}

	public void setImageUrl(String imageUrl, String thumbUrl) {
		this.imageUrl = imageUrl;
		this.thumbUrl = thumbUrl;
		this.setImage(this.thumbUrl);
	}

	public void setImageUrl(String url) {
		this.imageUrl = url;
		this.thumbUrl = url;
		this.setImage(this.thumbUrl);
	}

	public void setContainerSize(int containerWidth, int containerHeight) {
		this.containerWidth = containerWidth;
		this.containerHeight = containerHeight;
		contentPanel.setWidth(this.containerWidth + "px");
		contentPanel.setHeight(this.containerHeight + "px");
	}
	public void setImageSize(int imageWidth, int imageHeight) {
		this.imageWidth = imageWidth;
		this.imageHeight = imageHeight;
	}
}
